<?php

require_once 'model/storage.class.php';

/**
 *
 */
class Core_Table_Storage extends Database_Table
{

	/**
	 * @var string
	 */
	protected $_name = 'core_storage';

	/**
	 * @var string
	 */
	protected $_rowClass = 'Core_Table_Model_Storage';

	/**
	 * @param string int
	 * @return array
	 */
	function getDataForId($id)
	{

		$select = $this -> select() -> from(array('s' => Nuo::getT('core/storage'))) -> joinLeft(array('t' => Nuo::getT('storage_type')), 's.type_id = t.type_id') -> where('s.storage_id=?', (int)$id);

		$row = $this -> fetchRow($select);

		$data = json_decode($row -> extra, 1);
		$data['id'] = $row -> storage_id;
		$data['adapter_class'] = $row -> adapter_class;

		return $data;
	}

	/**
	 * get storage_id if it set to is_default =1
	 * get storage_id that is storable is 1
	 * @return int
	 *
	 */
	function getDefaultIds()
	{
		$select = $this -> select() -> from(array('s' => Nuo::getT('core/storage'))) -> joinLeft(array('t' => Nuo::getT('storage_type')), 's.type_id = t.type_id') -> where('s.is_default=1 AND s.is_storable=1');

		$row = $this -> fetchRow($select);

		if ($row)
		{
			$data[] = $row -> storage_id;
		}
		else
		{
			$select = $this -> select() -> from(array('s' => Nuo::getT('core/storage'))) -> joinLeft(array('t' => Nuo::getT('storage_type')), 's.type_id = t.type_id') -> where('s.is_storable=1');

			$data = array();

			foreach ($this->fetchAll($select) as $row)
			{
				$data[] = $row -> storage_id;
			}

		}
		return $data;
	}

}
